<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "search".
 *
 * @property int $id
 * @property string $keyword 关键字
 * @property int $total 搜索次数
 * @property int $user_id
 * @property string $create_time
 * @property string $ip IP
 * @property string $os 系统
 * @property string $browser 浏览器
 * @property string $mobile 手机类型
 * @property string $is_wx  是不是微信
 * @property string $result  搜索结果
 */
class Search extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'search';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['total', 'user_id'], 'integer'],
            [['create_time'], 'safe'],
            [['keyword'], 'string', 'max' => 255],
            [['ip', 'os', 'browser', 'mobile'], 'string', 'max' => 30],
            [['is_wx'], 'string', 'max' => 10],

            [['result'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'keyword' => 'Keyword',
            'total' => 'Total',
            'user_id' => 'User ID',
            'create_time' => 'Create Time',
            'ip' => 'Ip',
            'os' => 'Os',
            'browser' => 'Browser',
            'mobile' => 'Mobile',
            'is_wx' => 'Is Wx',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }
}
